<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Db;
use fast\Random;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends Backend
{

    protected $relationSearch = true;


    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            

            $vip_status = input('filter');
            $order_status = json_decode($vip_status,true);

            if(array_key_exists('order_status', $order_status)){
                $order_status = $order_status['order_status'];
                // 20是会员 ，30 是普通会员
                if($order_status=="20"){
                    $where2['vip_time'] = ['>',time()];
                }else if($order_status=="30"){
                    $where2['vip_time'] = NULL;
                }else{
                    $where2 = [];
                }
            }else{
                $where2 = [];
            }
           

            $total = $this->model
                ->with('group')
                ->where($where2)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('group')
                ->where($where2)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $k => $v) {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if ($params['password']) {
                    
                    $params['salt'] = Random::alnum();
                    $params['password'] = md5(md5($params['password']) . $params['salt']);
                } else {
                    unset($params['password'], $params['salt']);
                }
                //这里需要针对username和email做唯一验证
                $adminValidate = \think\Loader::validate('User');
                $adminValidate->rule([
                    'username' => 'require|unique:user',
                    'password' => 'regex:\S{32}',
                ]);
                $result = $row->validate('User.edit')->save($params);
                if ($result === false) {
                    $this->error($row->getError());
                }

                
                $this->success();
            }
            $this->error();
        }
       
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                
                $params['salt'] = Random::alnum();
                $params['password'] = md5(md5($params['password']) . $params['salt']);
                $params['status'] = 'normal';
                $result = $this->model->validate('User.add')->save($params);

                if ($result === false) {
                    $this->error($this->model->getError());
                }
                
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }
    

}
